version 14
set more off
set matsize 11000

***************************************************************************************************
* THIS ROUTINE GENERATES TABLE A.XII OF WASEEM (2019) RESTAT
***************************************************************************************************

/* TABLE */

#d				;
matrix 			input table=(0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0);
#d				cr

/* DEFINING TIME VARIABLE */		

use				"$project_data\ExemptionCutoff_06_11.dta",clear
keep			regnor year seinc employee incfrsalary nsale manuf regfor_sales medium_code
sort			regnor year
local 			var="seinc"
local			lbound=80000
local			ubound=500000
g				treat=employee==0

/* BINNING DATA */

local 			binsize=20000
local 			nbin=(`ubound'-`lbound')/`binsize'			
local 			mf=`binsize'/1000							
g 				bin=ceil(`var'/`binsize')
replace 		bin=bin*`mf'

/* LOG CHANGE IN EARNINGS */

g 				double z0=`var'
g 				z1=F1.`var'
g 				logchange=log(z1/z0)
keep 			if z0>80000 & z0<=500000
qui				sum logchange,d
drop			if (logchange<r(p1) | logchange>r(p99)) & year<2011

/* GENERATING TREATMENT DUMMIES */

g				cutoff=(`var'<=350000 & treat==1)
g				post=year>2008
g				y09=(year==2009)
g				y10=(year==2010)

/* COLUMN (1)-(2) MANUFACTURER - WITHOUT BASELINE INCOME CONTROL */

preserve
local			i=1
local			j=1
g				xtc=0
replace			xtc=1 if manuf>0 & manuf<. 
g				cutoff_09=cutoff*y09
g				cutoff_10=cutoff*y10
g				xtc_post=xtc*post
g				cutoff_xtc=cutoff*xtc
g				cutoff_xtc_09=cutoff*xtc*y09
g				cutoff_xtc_10=cutoff*xtc*y10
qui				sum xtc
matrix			table[13,`j']=r(mean)*100
matrix			table[13,`j'+1]=r(mean)*100
qui				reg logchange i.year cutoff xtc cutoff_09 cutoff_10 xtc_post cutoff_xtc cutoff_xtc_09 cutoff_xtc_10, cluster(regnor)
matrix			table[14,`j']=e(N)
matrix			table[14,`j'+1]=e(N)
matrix			table[`i',`j']=_b[cutoff_09]
matrix			table[`i'+1,`j']=_se[cutoff_09]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_10]
matrix			table[`i'+1,`j']=_se[cutoff_10]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc_09]
matrix			table[`i'+1,`j']=_se[cutoff_xtc_09]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc_10]
matrix			table[`i'+1,`j']=_se[cutoff_xtc_10]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc]
matrix			table[`i'+1,`j']=_se[cutoff_xtc]
local			i=`i'+2
matrix			table[`i',`j']=_b[xtc_post]
matrix			table[`i'+1,`j']=_se[xtc_post]
qui				sum logchange if post==0
matrix			table[15,`j']=r(mean)
matrix			table[15,`j'+1]=r(mean)
local			j=`j'+1

g				beta09_b=0
g				beta10_b=0
g				betaxtc09_b=0
g				betaxtc10_b=0
g				se09_b=0
g				se10_b=0
g				sextc09_b=0
g				sextc10_b=0
g				b_cutoff_xtc_b=0
g				se_cutoff_xtc_b=0
g				b_xtc_post_b=0
g				se_xtc_post_b=0
count			if xtc==1 & year==2009 
local			nxtc09=r(N)
count			if xtc==1 & year==2010 
local			nxtc10=r(N)
count			if xtc==1 & (year==2009 | year==2010) 
local			nxtc=r(N)
count			if xtc==0 & year==2009 
local			notxtc09=r(N)
count			if xtc==0 & year==2010 
local			notxtc10=r(N)
qui				count if xtc==1 & cutoff==1
local			ncutoffxtc=r(N)
forvalues		b=100(20)500 {
qui				count if xtc==1 & year==2009 & bin==`b'
local			nxtc09_b=r(N)
qui				count if xtc==1 & year==2010 & bin==`b'
local			nxtc10_b=r(N)
qui				count if xtc==1 & (year==2009 | year==2010) & bin==`b' 
local			nxtc_b=r(N)
qui				count if xtc==0 & year==2009 & bin==`b'
local			notxtc09_b=r(N)
qui				count if xtc==0 & year==2010 & bin==`b'
local			notxtc10_b=r(N)
qui				count if xtc==1 & cutoff==1 & bin==`b'
local			ncutoffxtc_b=r(N)
qui				reg logchange i.year cutoff xtc cutoff_09 cutoff_10 xtc_post cutoff_xtc cutoff_xtc_09 cutoff_xtc_10 if bin==`b', cluster(regnor)
replace			beta09_b=_b[cutoff_09]*`notxtc09_b'/`notxtc09' if bin==`b'
replace			se09_b=_se[cutoff_09]*`notxtc09_b'/`notxtc09' if bin==`b'
local			i=`i'+2
replace			beta10_b=_b[cutoff_10]*`notxtc10_b'/`notxtc10' if bin==`b'
replace			se10_b=_se[cutoff_10]*`notxtc10_b'/`notxtc10' if bin==`b'
local			i=`i'+2
replace			betaxtc09_b=_b[cutoff_xtc_09]*`nxtc09_b'/`nxtc09' if bin==`b'
replace			sextc09_b=_se[cutoff_xtc_09]*`nxtc09_b'/`nxtc09' if bin==`b'
local			i=`i'+2
replace			betaxtc10_b=_b[cutoff_xtc_10]*`nxtc10_b'/`nxtc10' if bin==`b'
replace			sextc10_b=_se[cutoff_xtc_10]*`nxtc10_b'/`nxtc10' if bin==`b'
local			i=`i'+2
replace			b_cutoff_xtc_b=_b[cutoff_xtc]*`ncutoffxtc_b'/`ncutoffxtc' if bin==`b'
replace			se_cutoff_xtc_b=_se[cutoff_xtc]*`ncutoffxtc_b'/`ncutoffxtc' if bin==`b'
replace			b_xtc_post_b=_b[xtc_post]*`nxtc_b'/`nxtc' if bin==`b'
replace			se_xtc_post_b=_se[xtc_post]*`nxtc_b'/`nxtc' if bin==`b'
}
local			i=1
duplicates  	drop bin, force
egen 			beta09=sum(beta09_b)
g				sse09_b=se09_b^2
egen			se09_s=sum(sse09_b)
g				se09=sqrt(se09_s)
egen 			beta10=sum(beta10_b)
g				sse10_b=se10_b^2
egen			se10_s=sum(sse10_b)
g				se10=sqrt(se10_s)
egen			betaxtc09=sum(betaxtc09_b)
g				ssextc09_b=sextc09_b^2
egen			sextc09_s=sum(ssextc09_b)
g				sextc09=sqrt(sextc09_s)
egen			betaxtc10=sum(betaxtc10_b)
g				ssextc10_b=sextc10_b^2
egen			sextc10_s=sum(ssextc10_b)
g				sextc10=sqrt(sextc10_s)
egen 			b_cutoff_xtc=sum(b_cutoff_xtc_b)
g				sse_cutoff_xtc_b=se_cutoff_xtc_b^2
egen			se_cutoff_xtc_s=sum(sse_cutoff_xtc_b)
g				se_cutoff_xtc=sqrt(se_cutoff_xtc_s)
egen			b_xtc_post=sum(b_xtc_post_b)
g				sse_xtc_post_b=se_xtc_post_b^2
egen			se_xtc_post_s=sum(sse_xtc_post_b)
g				se_xtc_post=sqrt(se_xtc_post_s)
qui				sum beta09
matrix			table[`i',`j']=r(mean)
qui				sum se09
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum beta10
matrix			table[`i',`j']=r(mean)
qui				sum se10
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum betaxtc09
matrix			table[`i',`j']=r(mean)
qui				sum sextc09
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum betaxtc10
matrix			table[`i',`j']=r(mean)
qui				sum sextc10
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum b_cutoff_xtc
matrix			table[`i',`j']=r(mean)
qui				sum se_cutoff_xtc
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum b_xtc_post
matrix			table[`i',`j']=r(mean)
qui				sum se_xtc_post
matrix			table[`i'+1,`j']=r(mean)
local 			j=`j'+1
restore

/* COLUMN (3)-(4) REGULAR TAX FILERS - WITHOUT BASELINE INCOME CONTROL */

preserve
local			i=1
bys				regnor:g nreturns=_N
g				xtc=(nreturns==6)
g				cutoff_09=cutoff*y09
g				cutoff_10=cutoff*y10
g				xtc_post=xtc*post
g				cutoff_xtc=cutoff*xtc
g				cutoff_xtc_09=cutoff*xtc*y09
g				cutoff_xtc_10=cutoff*xtc*y10
qui				sum xtc
matrix			table[13,`j']=r(mean)*100
matrix			table[13,`j'+1]=r(mean)*100
qui				reg logchange i.year cutoff xtc cutoff_09 cutoff_10 xtc_post cutoff_xtc cutoff_xtc_09 cutoff_xtc_10, cluster(regnor)
matrix			table[14,`j']=e(N)
matrix			table[14,`j'+1]=e(N)
matrix			table[`i',`j']=_b[cutoff_09]
matrix			table[`i'+1,`j']=_se[cutoff_09]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_10]
matrix			table[`i'+1,`j']=_se[cutoff_10]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc_09]
matrix			table[`i'+1,`j']=_se[cutoff_xtc_09]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc_10]
matrix			table[`i'+1,`j']=_se[cutoff_xtc_10]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc]
matrix			table[`i'+1,`j']=_se[cutoff_xtc]
local			i=`i'+2
matrix			table[`i',`j']=_b[xtc_post]
matrix			table[`i'+1,`j']=_se[xtc_post]
qui				sum logchange if post==0
matrix			table[15,`j']=r(mean)
matrix			table[15,`j'+1]=r(mean)
local			j=`j'+1

g				beta09_b=0
g				beta10_b=0
g				betaxtc09_b=0
g				betaxtc10_b=0
g				se09_b=0
g				se10_b=0
g				sextc09_b=0
g				sextc10_b=0
g				b_cutoff_xtc_b=0
g				se_cutoff_xtc_b=0
g				b_xtc_post_b=0
g				se_xtc_post_b=0
count			if xtc==1 & year==2009 
local			nxtc09=r(N)
count			if xtc==1 & year==2010 
local			nxtc10=r(N)
count			if xtc==1 & (year==2009 | year==2010) 
local			nxtc=r(N)
count			if xtc==0 & year==2009 
local			notxtc09=r(N)
count			if xtc==0 & year==2010 
local			notxtc10=r(N)
qui				count if xtc==1 & cutoff==1
local			ncutoffxtc=r(N)
forvalues		b=100(20)500 {
qui				count if xtc==1 & year==2009 & bin==`b'
local			nxtc09_b=r(N)
qui				count if xtc==1 & year==2010 & bin==`b'
local			nxtc10_b=r(N)
qui				count if xtc==1 & (year==2009 | year==2010) & bin==`b' 
local			nxtc_b=r(N)
qui				count if xtc==0 & year==2009 & bin==`b'
local			notxtc09_b=r(N)
qui				count if xtc==0 & year==2010 & bin==`b'
local			notxtc10_b=r(N)
qui				count if xtc==1 & cutoff==1 & bin==`b'
local			ncutoffxtc_b=r(N)
qui				reg logchange i.year cutoff xtc cutoff_09 cutoff_10 xtc_post cutoff_xtc cutoff_xtc_09 cutoff_xtc_10 if bin==`b', cluster(regnor)
replace			beta09_b=_b[cutoff_09]*`notxtc09_b'/`notxtc09' if bin==`b'
replace			se09_b=_se[cutoff_09]*`notxtc09_b'/`notxtc09' if bin==`b'
local			i=`i'+2
replace			beta10_b=_b[cutoff_10]*`notxtc10_b'/`notxtc10' if bin==`b'
replace			se10_b=_se[cutoff_10]*`notxtc10_b'/`notxtc10' if bin==`b'
local			i=`i'+2
replace			betaxtc09_b=_b[cutoff_xtc_09]*`nxtc09_b'/`nxtc09' if bin==`b'
replace			sextc09_b=_se[cutoff_xtc_09]*`nxtc09_b'/`nxtc09' if bin==`b'
local			i=`i'+2
replace			betaxtc10_b=_b[cutoff_xtc_10]*`nxtc10_b'/`nxtc10' if bin==`b'
replace			sextc10_b=_se[cutoff_xtc_10]*`nxtc10_b'/`nxtc10' if bin==`b'
local			i=`i'+2
replace			b_cutoff_xtc_b=_b[cutoff_xtc]*`ncutoffxtc_b'/`ncutoffxtc' if bin==`b'
replace			se_cutoff_xtc_b=_se[cutoff_xtc]*`ncutoffxtc_b'/`ncutoffxtc' if bin==`b'
replace			b_xtc_post_b=_b[xtc_post]*`nxtc_b'/`nxtc' if bin==`b'
replace			se_xtc_post_b=_se[xtc_post]*`nxtc_b'/`nxtc' if bin==`b'
}
local			i=1
duplicates  	drop bin, force
egen 			beta09=sum(beta09_b)
g				sse09_b=se09_b^2
egen			se09_s=sum(sse09_b)
g				se09=sqrt(se09_s)
egen 			beta10=sum(beta10_b)
g				sse10_b=se10_b^2
egen			se10_s=sum(sse10_b)
g				se10=sqrt(se10_s)
egen			betaxtc09=sum(betaxtc09_b)
g				ssextc09_b=sextc09_b^2
egen			sextc09_s=sum(ssextc09_b)
g				sextc09=sqrt(sextc09_s)
egen			betaxtc10=sum(betaxtc10_b)
g				ssextc10_b=sextc10_b^2
egen			sextc10_s=sum(ssextc10_b)
g				sextc10=sqrt(sextc10_s)
egen 			b_cutoff_xtc=sum(b_cutoff_xtc_b)
g				sse_cutoff_xtc_b=se_cutoff_xtc_b^2
egen			se_cutoff_xtc_s=sum(sse_cutoff_xtc_b)
g				se_cutoff_xtc=sqrt(se_cutoff_xtc_s)
egen			b_xtc_post=sum(b_xtc_post_b)
g				sse_xtc_post_b=se_xtc_post_b^2
egen			se_xtc_post_s=sum(sse_xtc_post_b)
g				se_xtc_post=sqrt(se_xtc_post_s)
qui				sum beta09
matrix			table[`i',`j']=r(mean)
qui				sum se09
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum beta10
matrix			table[`i',`j']=r(mean)
qui				sum se10
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum betaxtc09
matrix			table[`i',`j']=r(mean)
qui				sum sextc09
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum betaxtc10
matrix			table[`i',`j']=r(mean)
qui				sum sextc10
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum b_cutoff_xtc
matrix			table[`i',`j']=r(mean)
qui				sum se_cutoff_xtc
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum b_xtc_post
matrix			table[`i',`j']=r(mean)
qui				sum se_xtc_post
matrix			table[`i'+1,`j']=r(mean)
local 			j=`j'+1
restore

/* COLUMN (5)-(6) VAT-REGISTERED - WITHOUT BASELINE INCOME CONTROL */

preserve
local			i=1
g				xtc=(regfor_sales==1)
g				cutoff_09=cutoff*y09
g				cutoff_10=cutoff*y10
g				xtc_post=xtc*post
g				cutoff_xtc=cutoff*xtc
g				cutoff_xtc_09=cutoff*xtc*y09
g				cutoff_xtc_10=cutoff*xtc*y10
qui				sum xtc
matrix			table[13,`j']=r(mean)*100
matrix			table[13,`j'+1]=r(mean)*100
qui				reg logchange i.year cutoff xtc cutoff_09 cutoff_10 xtc_post cutoff_xtc cutoff_xtc_09 cutoff_xtc_10, cluster(regnor)
matrix			table[14,`j']=e(N)
matrix			table[14,`j'+1]=e(N)
matrix			table[`i',`j']=_b[cutoff_09]
matrix			table[`i'+1,`j']=_se[cutoff_09]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_10]
matrix			table[`i'+1,`j']=_se[cutoff_10]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc_09]
matrix			table[`i'+1,`j']=_se[cutoff_xtc_09]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc_10]
matrix			table[`i'+1,`j']=_se[cutoff_xtc_10]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc]
matrix			table[`i'+1,`j']=_se[cutoff_xtc]
local			i=`i'+2
matrix			table[`i',`j']=_b[xtc_post]
matrix			table[`i'+1,`j']=_se[xtc_post]
qui				sum logchange if post==0
matrix			table[15,`j']=r(mean)
matrix			table[15,`j'+1]=r(mean)
local			j=`j'+1

g				beta09_b=0
g				beta10_b=0
g				betaxtc09_b=0
g				betaxtc10_b=0
g				se09_b=0
g				se10_b=0
g				sextc09_b=0
g				sextc10_b=0
g				b_cutoff_xtc_b=0
g				se_cutoff_xtc_b=0
g				b_xtc_post_b=0
g				se_xtc_post_b=0
count			if xtc==1 & year==2009 
local			nxtc09=r(N)
count			if xtc==1 & year==2010 
local			nxtc10=r(N)
count			if xtc==1 & (year==2009 | year==2010) 
local			nxtc=r(N)
count			if xtc==0 & year==2009 
local			notxtc09=r(N)
count			if xtc==0 & year==2010 
local			notxtc10=r(N)
qui				count if xtc==1 & cutoff==1
local			ncutoffxtc=r(N)
forvalues		b=100(20)500 {
qui				count if xtc==1 & year==2009 & bin==`b'
local			nxtc09_b=r(N)
qui				count if xtc==1 & year==2010 & bin==`b'
local			nxtc10_b=r(N)
qui				count if xtc==1 & (year==2009 | year==2010) & bin==`b' 
local			nxtc_b=r(N)
qui				count if xtc==0 & year==2009 & bin==`b'
local			notxtc09_b=r(N)
qui				count if xtc==0 & year==2010 & bin==`b'
local			notxtc10_b=r(N)
qui				count if xtc==1 & cutoff==1 & bin==`b'
local			ncutoffxtc_b=r(N)
qui				reg logchange i.year cutoff xtc cutoff_09 cutoff_10 xtc_post cutoff_xtc cutoff_xtc_09 cutoff_xtc_10 if bin==`b', cluster(regnor)
replace			beta09_b=_b[cutoff_09]*`notxtc09_b'/`notxtc09' if bin==`b'
replace			se09_b=_se[cutoff_09]*`notxtc09_b'/`notxtc09' if bin==`b'
local			i=`i'+2
replace			beta10_b=_b[cutoff_10]*`notxtc10_b'/`notxtc10' if bin==`b'
replace			se10_b=_se[cutoff_10]*`notxtc10_b'/`notxtc10' if bin==`b'
local			i=`i'+2
replace			betaxtc09_b=_b[cutoff_xtc_09]*`nxtc09_b'/`nxtc09' if bin==`b'
replace			sextc09_b=_se[cutoff_xtc_09]*`nxtc09_b'/`nxtc09' if bin==`b'
local			i=`i'+2
replace			betaxtc10_b=_b[cutoff_xtc_10]*`nxtc10_b'/`nxtc10' if bin==`b'
replace			sextc10_b=_se[cutoff_xtc_10]*`nxtc10_b'/`nxtc10' if bin==`b'
local			i=`i'+2
replace			b_cutoff_xtc_b=_b[cutoff_xtc]*`ncutoffxtc_b'/`ncutoffxtc' if bin==`b'
replace			se_cutoff_xtc_b=_se[cutoff_xtc]*`ncutoffxtc_b'/`ncutoffxtc' if bin==`b'
replace			b_xtc_post_b=_b[xtc_post]*`nxtc_b'/`nxtc' if bin==`b'
replace			se_xtc_post_b=_se[xtc_post]*`nxtc_b'/`nxtc' if bin==`b'
}
local			i=1
duplicates  	drop bin, force
egen 			beta09=sum(beta09_b)
g				sse09_b=se09_b^2
egen			se09_s=sum(sse09_b)
g				se09=sqrt(se09_s)
egen 			beta10=sum(beta10_b)
g				sse10_b=se10_b^2
egen			se10_s=sum(sse10_b)
g				se10=sqrt(se10_s)
egen			betaxtc09=sum(betaxtc09_b)
g				ssextc09_b=sextc09_b^2
egen			sextc09_s=sum(ssextc09_b)
g				sextc09=sqrt(sextc09_s)
egen			betaxtc10=sum(betaxtc10_b)
g				ssextc10_b=sextc10_b^2
egen			sextc10_s=sum(ssextc10_b)
g				sextc10=sqrt(sextc10_s)
egen 			b_cutoff_xtc=sum(b_cutoff_xtc_b)
g				sse_cutoff_xtc_b=se_cutoff_xtc_b^2
egen			se_cutoff_xtc_s=sum(sse_cutoff_xtc_b)
g				se_cutoff_xtc=sqrt(se_cutoff_xtc_s)
egen			b_xtc_post=sum(b_xtc_post_b)
g				sse_xtc_post_b=se_xtc_post_b^2
egen			se_xtc_post_s=sum(sse_xtc_post_b)
g				se_xtc_post=sqrt(se_xtc_post_s)
qui				sum beta09
matrix			table[`i',`j']=r(mean)
qui				sum se09
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum beta10
matrix			table[`i',`j']=r(mean)
qui				sum se10
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum betaxtc09
matrix			table[`i',`j']=r(mean)
qui				sum sextc09
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum betaxtc10
matrix			table[`i',`j']=r(mean)
qui				sum sextc10
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum b_cutoff_xtc
matrix			table[`i',`j']=r(mean)
qui				sum se_cutoff_xtc
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum b_xtc_post
matrix			table[`i',`j']=r(mean)
qui				sum se_xtc_post
matrix			table[`i'+1,`j']=r(mean)
local 			j=`j'+1
restore

/* COLUMN (7)-(8) ELECTRONIC RETURN FILERS - WITHOUT BASELINE INCOME CONTROL */

preserve
local			i=1
g 				ereturn=(medium_code==5)
bys 			regno:egen n_ereturns=sum(ereturn)
g 				xtc=(n_ereturn>0)
g				cutoff_09=cutoff*y09
g				cutoff_10=cutoff*y10
g				xtc_post=xtc*post
g				cutoff_xtc=cutoff*xtc
g				cutoff_xtc_09=cutoff*xtc*y09
g				cutoff_xtc_10=cutoff*xtc*y10
qui				sum xtc
matrix			table[13,`j']=r(mean)*100
matrix			table[13,`j'+1]=r(mean)*100
qui				reg logchange i.year cutoff xtc cutoff_09 cutoff_10 xtc_post cutoff_xtc cutoff_xtc_09 cutoff_xtc_10, cluster(regnor)
matrix			table[14,`j']=e(N)
matrix			table[14,`j'+1]=e(N)
matrix			table[`i',`j']=_b[cutoff_09]
matrix			table[`i'+1,`j']=_se[cutoff_09]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_10]
matrix			table[`i'+1,`j']=_se[cutoff_10]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc_09]
matrix			table[`i'+1,`j']=_se[cutoff_xtc_09]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc_10]
matrix			table[`i'+1,`j']=_se[cutoff_xtc_10]
local			i=`i'+2
matrix			table[`i',`j']=_b[cutoff_xtc]
matrix			table[`i'+1,`j']=_se[cutoff_xtc]
local			i=`i'+2
matrix			table[`i',`j']=_b[xtc_post]
matrix			table[`i'+1,`j']=_se[xtc_post]
qui				sum logchange if post==0
matrix			table[15,`j']=r(mean)
matrix			table[15,`j'+1]=r(mean)
local			j=`j'+1

g				beta09_b=0
g				beta10_b=0
g				betaxtc09_b=0
g				betaxtc10_b=0
g				se09_b=0
g				se10_b=0
g				sextc09_b=0
g				sextc10_b=0
g				b_cutoff_xtc_b=0
g				se_cutoff_xtc_b=0
g				b_xtc_post_b=0
g				se_xtc_post_b=0
count			if xtc==1 & year==2009 
local			nxtc09=r(N)
count			if xtc==1 & year==2010 
local			nxtc10=r(N)
count			if xtc==1 & (year==2009 | year==2010) 
local			nxtc=r(N)
count			if xtc==0 & year==2009 
local			notxtc09=r(N)
count			if xtc==0 & year==2010 
local			notxtc10=r(N)
qui				count if xtc==1 & cutoff==1
local			ncutoffxtc=r(N)
forvalues		b=100(20)500 {
qui				count if xtc==1 & year==2009 & bin==`b'
local			nxtc09_b=r(N)
qui				count if xtc==1 & year==2010 & bin==`b'
local			nxtc10_b=r(N)
qui				count if xtc==1 & (year==2009 | year==2010) & bin==`b' 
local			nxtc_b=r(N)
qui				count if xtc==0 & year==2009 & bin==`b'
local			notxtc09_b=r(N)
qui				count if xtc==0 & year==2010 & bin==`b'
local			notxtc10_b=r(N)
qui				count if xtc==1 & cutoff==1 & bin==`b'
local			ncutoffxtc_b=r(N)
qui				reg logchange i.year cutoff xtc cutoff_09 cutoff_10 xtc_post cutoff_xtc cutoff_xtc_09 cutoff_xtc_10 if bin==`b', cluster(regnor)
replace			beta09_b=_b[cutoff_09]*`notxtc09_b'/`notxtc09' if bin==`b'
replace			se09_b=_se[cutoff_09]*`notxtc09_b'/`notxtc09' if bin==`b'
local			i=`i'+2
replace			beta10_b=_b[cutoff_10]*`notxtc10_b'/`notxtc10' if bin==`b'
replace			se10_b=_se[cutoff_10]*`notxtc10_b'/`notxtc10' if bin==`b'
local			i=`i'+2
replace			betaxtc09_b=_b[cutoff_xtc_09]*`nxtc09_b'/`nxtc09' if bin==`b'
replace			sextc09_b=_se[cutoff_xtc_09]*`nxtc09_b'/`nxtc09' if bin==`b'
local			i=`i'+2
replace			betaxtc10_b=_b[cutoff_xtc_10]*`nxtc10_b'/`nxtc10' if bin==`b'
replace			sextc10_b=_se[cutoff_xtc_10]*`nxtc10_b'/`nxtc10' if bin==`b'
local			i=`i'+2
replace			b_cutoff_xtc_b=_b[cutoff_xtc]*`ncutoffxtc_b'/`ncutoffxtc' if bin==`b'
replace			se_cutoff_xtc_b=_se[cutoff_xtc]*`ncutoffxtc_b'/`ncutoffxtc' if bin==`b'
replace			b_xtc_post_b=_b[xtc_post]*`nxtc_b'/`nxtc' if bin==`b'
replace			se_xtc_post_b=_se[xtc_post]*`nxtc_b'/`nxtc' if bin==`b'
}
local			i=1
duplicates  	drop bin, force
egen 			beta09=sum(beta09_b)
g				sse09_b=se09_b^2
egen			se09_s=sum(sse09_b)
g				se09=sqrt(se09_s)
egen 			beta10=sum(beta10_b)
g				sse10_b=se10_b^2
egen			se10_s=sum(sse10_b)
g				se10=sqrt(se10_s)
egen			betaxtc09=sum(betaxtc09_b)
g				ssextc09_b=sextc09_b^2
egen			sextc09_s=sum(ssextc09_b)
g				sextc09=sqrt(sextc09_s)
egen			betaxtc10=sum(betaxtc10_b)
g				ssextc10_b=sextc10_b^2
egen			sextc10_s=sum(ssextc10_b)
g				sextc10=sqrt(sextc10_s)
egen 			b_cutoff_xtc=sum(b_cutoff_xtc_b)
g				sse_cutoff_xtc_b=se_cutoff_xtc_b^2
egen			se_cutoff_xtc_s=sum(sse_cutoff_xtc_b)
g				se_cutoff_xtc=sqrt(se_cutoff_xtc_s)
egen			b_xtc_post=sum(b_xtc_post_b)
g				sse_xtc_post_b=se_xtc_post_b^2
egen			se_xtc_post_s=sum(sse_xtc_post_b)
g				se_xtc_post=sqrt(se_xtc_post_s)
qui				sum beta09
matrix			table[`i',`j']=r(mean)
qui				sum se09
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum beta10
matrix			table[`i',`j']=r(mean)
qui				sum se10
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum betaxtc09
matrix			table[`i',`j']=r(mean)
qui				sum sextc09
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum betaxtc10
matrix			table[`i',`j']=r(mean)
qui				sum sextc10
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum b_cutoff_xtc
matrix			table[`i',`j']=r(mean)
qui				sum se_cutoff_xtc
matrix			table[`i'+1,`j']=r(mean)
local			i=`i'+2
qui				sum b_xtc_post
matrix			table[`i',`j']=r(mean)
qui				sum se_xtc_post
matrix			table[`i'+1,`j']=r(mean)
local 			j=`j'+1
restore

/* FORMATTING TABLE */

svmat 			table
keep			tab*
keep			if _n<=15
forvalues		c=1/8 {
				g c`c'=string(table`c',"%9.3f")
				replace c`c'=string(table`c',"%9.1f") in 13
				replace c`c'=string(table`c',"%9.0fc") in 14
				forvalues		r=2(2)12 {
					replace c`c'="("+string(table`c',"%9.3f")+")" in `r'
				}
}
g				c9=""
replace			c9="$\mbox{\emph{to-zero}} \times 2009$" in 1
replace			c9="$\mbox{\emph{to-zero}} \times 2010$" in 3
replace			c9="$\mbox{\emph{to-zero}} \times \mbox{\emph{trait}} \times 2009$" in 5
replace			c9="$\mbox{\emph{to-zero}} \times \mbox{\emph{trait}} \times 2010$" in 7
replace			c9="$\mbox{\emph{to-zero}} \times \mbox{\emph{trait}}$" in 9
replace			c9="$\mbox{\emph{trait}} \times \mbox{\emph{post}}$" in 11
replace			c9="Observations" in 14
forvalues		s=1/8 {
g				s`s'="&"
}
g				l="\\"
replace 		l="\\\\\\" in 12
browse			c9 s1 c1 s2 c2 s3 c3 s4 c4 s5 c5 s6 c6 s7 c7 s8 c8 l
